<?php

Raise::load('core.RaiseObject');

/**
 * RaiseConsoleApplication class
 * represents a PHP application that is accessed via the console
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core
 * @since 1.2
 */
class RaiseConsoleApplication extends RaiseObject {

    /**
     * A collection of arguments passed into the script
     * @var RaiseCollection
     */
    private $arguments;

    /**
     * Create a new RaiseConsoleApplication
     * @param array $argv The variable $argv passed into the script
     */
    public function __construct($argv){
        if(php_sapi_name() != 'cli'){
            throw new RaiseInvalidRequestException('RaiseConsoleApplication cannot run from a non-Command Line Interface (CLI).');
        }
        $this->arguments = self::parseArgs($argv);
    }

    /**
     * Get the arguments passed to this console application
     * @return RaiseCollection
     */
    public function arguments(){
        return $this->arguments;
    }

    private static function parseArgs($argv){
        array_shift($argv);
        $out = array();
        foreach ($argv as $a){
            if (substr($a, 0, 2) == '--'){
                $eqPos = strpos($a, '=');
                if ($eqPos === false){
                    $key = substr($a, 2);
                    $out[$key] = isset($out[$key]) ? $out[$key] : true;
                } else {
                    $key = substr($a, 2, $eqPos - 2);
                    $out[$key] = substr($a, $eqPos + 1);
                }
            } else if (substr($a, 0, 1) == '-'){
                if (substr($a, 2, 1) == '='){
                    $key = substr($a, 1, 1);
                    $out[$key] = substr($a, 3);
                } else {
                    $chars = str_split(substr($a, 1));
                    foreach ($chars as $c){
                        $key = $c;
                        $out[$key] = isset($out[$key]) ? $out[$key] : true;
                    }
                }
            } else {
                $out[] = $arg;
            }
        }
        return new RaiseCollection($out);
    }

    public function write($s){
        echo $s;
    }

    public function writeLine($s){
        echo $s . RaiseText::NEWLINE_NIX;
    }

    public function readLine(){
        static $buffer = '';

        $fp = fopen('php://stdin', 'r');
        $line = $buffer;
        while($l = fgets($fp, 1024)){
            $l = RaiseText::neutralizeNewlines($l);
            $line .=  $l;
            $newLinePos = strpos($line, RaiseText::NEWLINE_NIX);
            if($newLinePos !== false){
                break;
            }
        }
        fclose($fp);
        $newLinePos = strpos($line, RaiseText::NEWLINE_NIX);
        if($newLinePos === false){
            $buffer = '';
        }else{
            $buffer = substr($line, $newLinePos + 1);
            $line = substr($line, 0, $newLinePos);
        }
        return $line;
    }

}
